//
//  UserInformationEditingViewController.m
//  PPScaleDemo
//
//  Created by 彭思远 on 2021/3/11.
//

#import "UserInformationEditingViewController.h"
#import "ChangeDNSViewController.h"

@interface UserInformationEditingViewController ()
@property (weak, nonatomic) IBOutlet UITextField *heightTF;
@property (weak, nonatomic) IBOutlet UITextField *ageTF;
@property (weak, nonatomic) IBOutlet UISegmentedControl *genderSegment;
@property (weak, nonatomic) IBOutlet UISegmentedControl *unitSigment;
@property (weak, nonatomic) IBOutlet UISegmentedControl *pregnantModeSigment;
@property (weak, nonatomic) IBOutlet UISegmentedControl *athleteModeSigment;
@property (weak, nonatomic) IBOutlet UITextField *baseUrlTF;

@end

@implementation UserInformationEditingViewController

+ (instancetype)instance{
    
    return [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"UserInformationEditingViewController"];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"User Information Editing";
    UserModel *currentUser = [IdentityManager manager].currentUser;
    if (currentUser) {
        self.heightTF.text = [NSString stringWithFormat:@"%ld", currentUser.userHeight];
        self.ageTF.text = [NSString stringWithFormat:@"%ld", currentUser.userAge];
        self.genderSegment.selectedSegmentIndex = currentUser.userGender;
        self.unitSigment.selectedSegmentIndex = currentUser.userUnit;
        self.pregnantModeSigment.selectedSegmentIndex = currentUser.isPregnantMode;
        self.athleteModeSigment.selectedSegmentIndex = currentUser.isAthleteMode;
        self.baseUrlTF.text = currentUser.baseUrl;
    }
}

- (IBAction)saveBtnClick:(id)sender {
    UserModel *currentUser = [IdentityManager manager].currentUser;
    if (!currentUser) {
        currentUser = [[UserModel alloc] init];
    }
    currentUser.userAge = self.ageTF.text.integerValue;
    currentUser.userHeight = self.heightTF.text.integerValue;
    currentUser.userGender = self.genderSegment.selectedSegmentIndex;
    currentUser.userUnit = self.unitSigment.selectedSegmentIndex;
    currentUser.isAthleteMode = self.athleteModeSigment.selectedSegmentIndex;
    currentUser.isPregnantMode = self.pregnantModeSigment.selectedSegmentIndex;
    currentUser.baseUrl = self.baseUrlTF.text;
    [IdentityManager manager].currentUser = currentUser;
    [[IdentityManager manager] saveCurrentUserData];
    [[HttpRequest request] cleanAllTask];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"userCreatSuccess" object:nil];
}

- (IBAction)changeDNSBtnClick:(id)sender {
    ChangeDNSViewController *vc = [ChangeDNSViewController instance];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
